package net.zigin.epi.demo.service.impl;

import net.zigin.commons.util.*;
import net.zigin.epi.demo.vo.SysUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.zigin.epi.demo.dao.BaseMapper;
import net.zigin.epi.demo.dao.SysUserMapper;
import net.zigin.epi.demo.model.SysUser;
import net.zigin.epi.demo.service.SysUserService;

import java.util.Date;
import java.util.List;
import java.util.Map;

/*#################################################################*
# 说明:以下代码通过哲勤代码生成工具自动生成    
# 工具:GeneratorCode
# 作者:mgw1986                                                  
# 生成时间:2021-8-24 18:41:47                                               
##################################################################*/


/**
 * 用户信息服务实现类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: SysUserServiceImpl
 * @date 2021-8-24 18:41:47
 */
@Service
public class SysUserServiceImpl extends AbstractServiceImpl<SysUser, String> implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public BaseMapper<SysUser, String> getMapper() {
        return sysUserMapper;
    }

    /**
     * 根据用户编号查询用户信息
     *
     * @param id 用户编号
     * @return
     */
    @Override
    public SysUserVo getSysUserInfoById(String id) {
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(id);
        if (null == sysUser) {
            return null;
        }
        SysUserVo sysUserVo = ReflectUtil.copyProperties(sysUser, SysUserVo.class);
        return sysUserVo;
    }

    /**
     * 分页查询用户信息
     *
     * @param deptId    部门编号
     * @param status    状态:0-正常 1-禁用 2-删除
     * @param pageIndex 页码
     * @param pageSize  每页的行数
     * @return
     */
    @Override
    public List<SysUserVo> getSysUserList(String deptId, String status, int pageIndex, int pageSize) {
        Map<String, Object> paramMap = ParamsMapUtil.getInstance()
                .addParam("deptId", deptId)
                .addParam("status", status)
                .addParam("pageIndex", pageIndex)
                .addParam("pageSize", pageSize);
        List<SysUser> sysUserList = sysUserMapper.selectListByCondition(paramMap);

        List<SysUserVo> sysUserVoList = ReflectUtil.copyProperties(sysUserList, SysUserVo.class);
        return sysUserVoList;
    }

    /**
     * 查询用户信息总数
     *
     * @param deptId 部门编号
     * @param status 状态:0-正常 1-禁用 2-删除
     * @return
     */
    @Override
    public int getSysUserCount(String deptId, String status) {
        Map<String, Object> paramMap = ParamsMapUtil.getInstance()
                .addParam("deptId", deptId)
                .addParam("status", status);
        return sysUserMapper.selectCount(paramMap);
    }

    /**
     * 新增或编辑用户信息
     *
     * @param sysUserVo 用户信息
     * @return
     */
    @Override
    public void saveOrUpdateSysUser(SysUserVo sysUserVo) {
        AssertUtil.isNull(sysUserVo, "用户信息不能为空");

        //新增
        if (null == sysUserVo.getId()) {
            sysUserVo.setId(StringUtil.getOrder32UUID());
            sysUserVo.setCreateTime(new Date());

            SysUser sysUser = ReflectUtil.copyProperties(sysUserVo, SysUser.class);
            sysUserMapper.insertSelective(sysUser);
            return;
        }
        //编辑
        sysUserVo.setModifyTime(new Date());
        SysUser sysUser = ReflectUtil.copyProperties(sysUserVo, SysUser.class);
        sysUserMapper.updateByPrimaryKey(sysUser);
        //或者如果只更新不为空的字段，使用
        //sysUserMapper.updateByPrimaryKeySelective(sysUser);
    }
}